package online.misscl.lsy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import online.misscl.lsy.entity.dto.vo.TableDataInfoVo;
import online.misscl.lsy.util.AjaxResult;
import online.misscl.lsy.util.JwtUtils;
import online.misscl.lsy.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class BaseController {

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 响应请求分页数据
     */
    protected TableDataInfoVo getDataTable(Page page)
    {
        TableDataInfoVo rspData = new TableDataInfoVo();
        rspData.setCode(ResultCode.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    protected TableDataInfoVo getDataTable(List rows, long total) {
        TableDataInfoVo rspData = new TableDataInfoVo();
        rspData.setCode(ResultCode.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(rows);
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 返回成功
     */
    public AjaxResult success(){
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(){
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message){
        return AjaxResult.success(message);
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(Object data){
        return AjaxResult.success(data);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message){
        return AjaxResult.error(message);
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows){
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result) {
        return result ? success() : error();
    }

    /**
     * 获取当前登录id
     * @return
     */
    public Integer getUserId(){
        return JwtUtils.getUserId(httpServletRequest);
    }

    /**
     * 获取当前登录auth
     * @return
     */
    public static Integer getAuth(HttpServletRequest httpServletRequest){
        return JwtUtils.getAuth(httpServletRequest);
    }

}
